//
//  AllGuiderVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/8/30.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "AllGuiderVC.h"
#import "GuiderListCell.h"
#import "GuiderCardModel.h"
#import "GuiderDetailVC.h"
@interface AllGuiderVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong)UICollectionView * collectionView;



@property (nonatomic, strong)UIImageView * imageView;
@end

@implementation AllGuiderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupBg];
    [self setup];
    [self loadDataSource];
}


- (void)setupBg {
    
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _imageView.image = self.Bgimage;
    _imageView.backgroundColor = kThemeColor;
    [self.view addSubview:_imageView];
}

- (void)setup {
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(20,kNavigationBarHeight, SCREEN_WIDTH - 40, SCREEN_HEIGHT - kNavigationBarHeight) collectionViewLayout:layout];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"GuiderListCell" bundle:nil] forCellWithReuseIdentifier:@"GuiderListCell"];
    self.title = MYLocalizedString(@"GUIDELIST_HOTGUIDER", nil);
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}


- (void)loadDataSource {
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    
    [NetAPIManager getAllGuidersBlock:^(NSArray *guiderArray, NSError *error) {
       
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        self.dataSource =  [NSMutableArray arrayWithArray:guiderArray];
        [self.collectionView reloadData];
    }];
}

#pragma mark  --UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GuiderListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GuiderListCell" forIndexPath:indexPath];
    
    GuiderCardModel * model = self.dataSource[indexPath.row];
    
    [cell configDataWithGuiderModel:model];
    
    return cell;
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return CGSizeMake(SCREEN_WIDTH - 40, 172);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
 
    return 0.000001;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    GuiderCardModel *model = self.dataSource[indexPath.row];
    
    GuiderDetailVC * detailVC = [[GuiderDetailVC alloc] init];
    
    detailVC.guiderID = model.guideid;
    //detailVC.delegate = self;
    [self.navigationController pushViewController:detailVC animated:YES];
    [MobClick event:@"8_1_2"];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
